package com.xysq.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.rockcent.model.TipsModel;
import com.xysq.XYApplication;
import com.xysq.lemon.R;
import com.xysq.util.PropertiesUtil;
import com.xysq.widget.CircleImageView;

import java.util.List;

/**
 * Created by Steward_Han on 2015/10/10.
 */
public class TipsAdapter extends BaseAdapter{
    ViewHolder viewHolder;
    Context context;
    List<TipsModel> tipsModels;

    public TipsAdapter(Context context, List<TipsModel> tipsModels){
        this.context = context;
        this.tipsModels = tipsModels;
    }

    @Override
    public int getCount() {
        return tipsModels.size();
    }

    @Override
    public Object getItem(int position) {
        return tipsModels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View contentView, ViewGroup viewGroup) {
        viewHolder = new ViewHolder();
        if(null == contentView){
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            contentView = layoutInflater.inflate(R.layout.item_tips, null);

            viewHolder.headImg = (CircleImageView)contentView.findViewById(R.id.img_head);
            viewHolder.logoImg = (ImageView)contentView.findViewById(R.id.img_logo);
            viewHolder.contentTxt = (TextView)contentView.findViewById(R.id.txt_content);
            viewHolder.nameTxt = (TextView)contentView.findViewById(R.id.txt_name);
            viewHolder.schoolTxt = (TextView)contentView.findViewById(R.id.txt_school);
            viewHolder.titleTxt = (TextView)contentView.findViewById(R.id.txt_title);
            viewHolder.timesTxt = (TextView)contentView.findViewById(R.id.txt_times);

            contentView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder)contentView.getTag();
        }

        TipsModel tipsModel = tipsModels.get(position);
        if(!TextUtils.isEmpty(tipsModel.getHeadUrl()) && !tipsModel.getHeadUrl().equals("null")) {
            XYApplication.getImageLoader().displayImage(PropertiesUtil.getImageServer() + tipsModel.getHeadUrl(), viewHolder.headImg);
        }else {
            viewHolder.headImg.setImageResource(R.drawable.def_img_head);
        }
    if(!TextUtils.isEmpty(tipsModel.getLogoUrl()) && !tipsModel.getLogoUrl().equals("null")) {
            XYApplication.getImageLoader().displayImage(PropertiesUtil.getImageServer() + tipsModel.getLogoUrl(), viewHolder.logoImg);
        }
        viewHolder.contentTxt.setText(tipsModel.getContent());
        viewHolder.nameTxt.setText(tipsModel.getName());
        viewHolder.schoolTxt.setText(tipsModel.getSchool());
        viewHolder.titleTxt.setText(tipsModel.getTitle());
        viewHolder.timesTxt.setText("已被打赏" + tipsModel.getTimes() + "次");
        return contentView;
    }

    class ViewHolder{
        CircleImageView headImg;
        TextView nameTxt;
        TextView schoolTxt;
        ImageView logoImg;
        TextView titleTxt;
        TextView contentTxt;
        TextView timesTxt;
    }
}
